package com.jtpay.payresulthelper.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    private static long DAY_SECONDS = 60 * 60 * 24 * 1000L;
    private static long DAY_SECONDS_OFFSET = 60 * 60 * 8 * 1000L;
    private static DateFormat sSimpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    private static final String TIME_FORMAT = "yyyyMMddHHmmss";
    private static DateFormat sTimeFormat = new SimpleDateFormat(TIME_FORMAT);
    private static long HALF_HOUR = 30 * 60 * 1000L;
    private static Calendar mCalendar = Calendar.getInstance();

    /**
     * get Current time stamp
     *
     * @return
     */
    public static long getCurrentTimeStamp() {
        long now = System.currentTimeMillis();
        return now;
    }

    /**
     * get half hour time stamp
     *
     * @return
     */
    public static long getHalfHourTimeStamp() {
        long before = System.currentTimeMillis() - HALF_HOUR;
        return before;
    }

    /**
     * get Current day zero time stamp
     *
     * @return
     */
    public static long getDateZeroTimeStamp() {
        long now = System.currentTimeMillis();
        long zero = now - (now + DAY_SECONDS_OFFSET) % DAY_SECONDS;
        return zero;
    }

    /**
     * @return 获取当前时间 Eg:20180912
     */
    public static String getDateTime() {
        return sSimpleDateFormat.format(new Date());
    }

    //通过字符串20180909111122 这个格式的字符串转为时间戳
    public static long stringToTimeStamp(String string) {
        try {
            Date date = sTimeFormat.parse(string);
            mCalendar.setTime(date);
            return mCalendar.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("Time format is not ok");
        }
    }
}
